package com.aidex.admin.controller.system;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.query.QueryGenerator;
import com.aidex.common.utils.StringUtils;
import com.aidex.system.domain.SysNotice;
import com.aidex.system.service.SysNoticeService;
import com.aidex.system.service.SysNoticeUserReadService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 公告 信息操作处理
 */
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController<SysNoticeService> {
    @Autowired
    private SysNoticeUserReadService sysNoticeUserReadService;

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/page")
    public R page(SysNotice entity,
                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                  HttpServletRequest req) {
        QueryWrapper<SysNotice> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        Page<SysNotice> page = new Page<>(pageNo, pageSize);
        IPage<SysNotice> pageList = service.selectPage(page, queryWrapper);
        return R.data(pageList);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public R getInfo(@PathVariable String noticeId) {
        return R.data(service.selectById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "通知公告" , businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody SysNotice entity) {
        if(StringUtils.isEmpty(entity.getId())) {
            return R.status(service.insert(entity));
        }
        return R.status(service.updateById(entity));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "通知公告" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public R remove(@PathVariable String[] noticeIds) {
        return R.status(service.deleteBatchIds(Arrays.asList(noticeIds)));
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @GetMapping(value = "getNoticeView/{noticeId}")
    public R getNoticeView(@PathVariable String noticeId) {
        //标记当前登录人已读改消息
        sysNoticeUserReadService.setIsRead(noticeId);
        return R.data(service.selectById(noticeId));
    }

    /**
     * 获取个人公告阅读列表
     */
    @GetMapping("listNoticeByUser/page")
    public R listNoticeByUser(SysNotice entity,
                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                              HttpServletRequest req) {
        return R.data("");
    }

    /**
     * 将通知公告标记为已读
     */
    @Log(title = "公告标记为已读" , businessType = BusinessType.UPDATE)
    @PutMapping("updateNoticeToRead/{noticeIds}")
    public R updateNoticeToRead(@PathVariable String[] noticeIds) {
        if (noticeIds != null && noticeIds.length > 0) {
            for (int i = 0; i < noticeIds.length; i++) {
                sysNoticeUserReadService.setIsRead(noticeIds[i]);
            }
        }
        return R.status(1);
    }
}
